package cn.jee.controller;

import cn.jee.entity.Stu;
import cn.jee.repository.StuRepository;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller

public class StuController {
  @Autowired
  StuRepository stuRepository;
  @RequestMapping ("/stu_input")
  public String StuInput(Model model) {
    model.addAttribute("stu", new Stu());
    return "addStu";
  }

  @RequestMapping ("/addStu")
  public String addStu(@Valid Stu stu, BindingResult bindingResult, Model model) {
    if (bindingResult.hasErrors()) {
      model.addAttribute("stu", stu);
      return "addStu";
    }
    stuRepository.save(stu);
    return "redirect:/";
  }



}
